package cn.tworice.blog.web;

import cn.tworice.blog.entity.QRCode;
import cn.tworice.blog.service.QRCodeService;
import cn.tworice.blog.service.ToolsService;
import cn.tworice.blog.service.impl.ToolsServiceImpl;
import cn.tworice.blog.task.MultithreadScheduleTask;
import cn.tworice.blog.utils.QRCodeUtil;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "二维码模块",tags = {"二维码模块接口"})
@Slf4j
@RestController
@RequestMapping("api/qrcode")
@CrossOrigin
public class QRCodeController {

    @Autowired
    private QRCodeService qrCodeService;

    // 二维码存储地址
    @Value("${blog.QRCodeFile}")
    private String filePath;

    // 二维码访问地址
    @Value("${blog.QRCodeAccess}")
    private String accessPath;



    /**
     * 生成二维码
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @PostMapping("encode")
    @ApiOperation("生成二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logo",value = "LOGO图片",dataType = "file",dataTypeClass = MultipartFile.class),
            @ApiImplicitParam(name = "content",value = "二维码存储内容(<500)",required = true,dataType = "string",dataTypeClass = String.class),
            @ApiImplicitParam(name = "color",value = "前景色(Hex)",dataType = "string",dataTypeClass = String.class),
            @ApiImplicitParam(name = "bgc",value = "背景色(Hex)",dataType = "string",dataTypeClass = String.class),
            @ApiImplicitParam(name = "margin",value = "边框(取值1-10)",dataType = "int",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "isCompress",value = "LOGO图片是否压缩",defaultValue = "true",dataType = "boolean",dataTypeClass = Boolean.class),
            @ApiImplicitParam(name = "level",value = "纠错等级-取值L·M·Q·H",defaultValue = "H",dataType = "ErrorCorrectionLevel",dataTypeClass = ErrorCorrectionLevel.class),
            @ApiImplicitParam(name = "size",value = "取值100-1000",defaultValue = "300",dataType = "int",dataTypeClass = Integer.class),
    })
    public RequestResult encode(MultipartFile logo, QRCode qrCode){
        RequestResult result = new RequestResult();
        try {
            String logoPath = qrCodeService.saveFile(logo);
            String path=qrCodeService.createCode(qrCode,logoPath);
            result.setCode(new StatusCode(200,"生成二维码成功"));
            result.appendData("filePath",accessPath+path);
        }catch (Exception ex){
            result.setCode(new StatusCode(500,"生成二维码异常"));
            log.error("生成二维码出现异常"+ex.getMessage());
        }
        return result;
    }

    @PostMapping("decode")
    @ApiOperation("解析二维码")
    public RequestResult decode(MultipartFile file) {

        RequestResult result = new RequestResult();
        try {
            String data = qrCodeService.parseCode(file);
            result.setCode(new StatusCode(200, "操作成功"));
            result.appendData("result", data);
        } catch (Exception ex) {
            result.setCode(new StatusCode(500, "解析二维码异常"));
            log.error("解析二维码出现异常" + ex.getMessage());
        }
        return result;
    }

    /**
     * 手动清理二维码目录
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @PostMapping("/admin/clearQRCodeFiles")
    @ApiOperation("手动清理二维码目录下文件")
    public RequestResult clearQRCodeFiles(){

        return qrCodeService.clearQRCodeFiles();
    }


}
